// Created by Roman Novitsky on 02.06.2009


#region Imported Namespaces


using System.Collections.Generic;
using System.Windows.Input;


#endregion


namespace WtfManager.UIManager.Commands
{
  public class OpenCommandHandler: OpenCommandHandlerBase
  {
    #region Properties/Indexers


    ///<summary>
    /// Gets a <see cref="ICommand"/> that is handled by this handler.
    ///</summary>
    public override ICommand Command
    {
      get
      {
        return ApplicationCommands.Open;
      }
    }

    /// <summary>
    /// Gets a description of a service.
    /// </summary>
    public override string Description
    {
      get
      {
        return "Handles ApplicationCommands.Open command.";
      }
    }

    /// <summary>
    /// Gets a name of a service.
    /// </summary>
    public override string Name
    {
      get
      {
        return "Open Application Command Handler";
      }
    }


    #endregion


    #region Methods


    protected override bool CanExecute(List<object> itemsToOpen)
    {
      if (itemsToOpen.Count == 1)
      {
        return CanOpenInCurrentTab(itemsToOpen[0]) || CanOpenInNewTab(itemsToOpen[0]) || CanLaunch(itemsToOpen[0]);
      }

      foreach (object item in itemsToOpen)
      {
        if (CanOpenInNewTab(item) || CanLaunch(item))
        {
          return true;
        }
      }

      return false;
    }


    protected override void Execute(List<object> itemsToOpen)
    {
      if (itemsToOpen.Count == 1)
      {
        object item = itemsToOpen[0];

        if (OpenInCurrentTab(item))
        {
          return;
        }

        Launch(item);
      }
      else
      {
        foreach (object item in itemsToOpen)
        {
          if (OpenInNewTab(item))
          {
            continue;
          }

          Launch(item);
        }
      }
    }


    #endregion
  }
}